package game.logic;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import game.logic.block.Block;
import game.util.Game;

public class AreaModelBlockPoint implements AreaModel{

	
	List<BlockPoint> list;
	
	@Override
	public Iterator<BlockPoint> iterator() {
		return list.iterator();
	}

	@Override
	public void init() {
		list = new ArrayList<BlockPoint>();
	}

	@Override
	public void add(Block block) {
		for(BlockPoint point : block){
			list.add(point);
		}
		Collections.sort(list);
	}

	@Override
	public void clear() {
		int full = 0;
		for (int i = 0;i < list.size();i++)
		{
			BlockPoint point = list.get(i);
			if(i + Game.COLNUM - 1 < list.size())
			{
				BlockPoint pointAfter = list.get(i + Game.COLNUM - 1);
				if(point.y == pointAfter.y)
				{
					full++;
					for(int j = 0;j<Game.COLNUM;j++)
					{
						list.remove(i);
					}
					Collections.sort(list);
					i--;
					continue;
				}
				
			}
			point.y += full;
		}
	}

	@Override
	public boolean isDeath() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean checkHit(Block block) {
		if( block.hit())
		return true;
		for(BlockPoint point: list)
		{
			if( block.hit(point.x, point.y))
				return true;
		}
		return false;
	}

	public void print() {
		System.out.println("All points are:");
		for(BlockPoint bp : list)
		{
			System.out.println("POINT:" + bp.x + "," + bp.y + ".");
		}
	}

	public List<BlockPoint>  getList(){
		return list;
	}
	

}
